//通过auth.js获取到的服务器返回的资料

// console.log(userInfo);

$("#top-username").click(function () {
  if (!confirm("是否退出登录？")) return;
  localStorage.removeItem("token");
  location.reload();
});

render();
//渲染页面
function render() {
  getUser();
  navInfo();
  testsType();
}

//用户信息渲染
function navInfo() {
  $("#setting-phone").text(userInfo.phone);
  $("#top-username").text(userInfo.name);
  $("#top-img").attr("src", userInfo.avatar);
  $(".img-user-setting").attr("src", userInfo.avatar);
}
let avatar = userInfo.avatar;

// 修改资料
$("#settingBtn").click(function () {
  let { _id } = userInfo;
  $.ajax({
    url: "/students/changeStudent",
    type: "POST",
    data: {
      _id,
      avatar,
      gender: $(`input[name='sex']:checked`).val(),
      name: $("#setting-name").val()
    },
    success(res) {
      alert(res.message);
      render();
    }
  });
});

//图片上传
$("#upImg").change(function (e) {
  const files = e.target.files;
  console.log(files);
  const fd = new FormData();
  fd.append("file", files[0]);
  $.ajax({
    url: "/images/uploadImages",
    type: "POST",
    data: fd,
    contentType: false,
    processData: false,
    cache: false,
    success(res) {
      console.log("上传结果", res);
      avatar = res.data;
      $(".img-user-setting").attr("src", res.data);
    }
  });
});

// 获取考试类型
function testsType() {
  $.ajax({
    url: "/tests/getTests",
    type: "post",
    success(res) {
      $("#test-type1").attr("href", `./start-test.html?${res[1]._id}`);
      $("#test-type2").attr("href", `./classify-1.html?${res[4]._id}`);
      console.log("考试类型", res);
    }
  });
}
